package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.VisitorReward;
import com.sl.au.service.UserService;
import com.sl.au.service.VisitorRewardService;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping("/ui/visitorReward")
public class VisitorRewardController extends BaseController {

	@Autowired
	private VisitorRewardService visitorRewardService;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private UserService userService;

	@RequestMapping("/findMe/{visitorId}")
	public MsgHelper findMe(@PathVariable("visitorId") String id) {
		return MsgHelper.success().put("visitor", userService.getRepository().findOne(id));
	}

	@RequestMapping("/findMyDetails/{userId}")
	public MsgHelper findMyDetails(@PathVariable("userId") String userId) {
		List<VisitorReward> rewards = visitorRewardService.findRewardOnlyKey(0, Integer.MAX_VALUE, userId);
		return MsgHelper.success().put("rewards", rewards);

	}

	@RequestMapping(value = "/getDetails", method = RequestMethod.GET)
	public Map<String, Object> getDetails(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");// 传递过来的用户ID
		String con = request.getParameter("con");
		int count = 0;
		List<VisitorReward> infos = null;
		if (StringUtils.hasText(con)) {
			infos = visitorRewardService.findRewardByKeyAndCon(start, length, key, con);
			count = visitorRewardService.findRewardCountByKeyAndCon(key, con);
		} else {
			infos = visitorRewardService.findRewardOnlyKey(start, length, key);
			count = visitorRewardService.findRewardCountOnlyKey(key);
		}

		map.put("data", infos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 * select u.id as id, u.`name` as name,u.sContactType as contactType,u.sex
	 * as sex,sum(r.rewardValue) as sum from sl_wcsi_visitor_reward r LEFT JOIN
	 * sl_au_user u on u.id=r.userId GROUP BY r.userId having (u.`name` like
	 * '%b%' or u.sContactType like '%b%' or u.sex like '%b%')
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");

		String sql = "select u.id as id, u.`name` as name,u.sContactType as contactType,u.sex as sex,sum(r.rewardValue) as sum "
				+ "from sl_wcsi_visitor_reward r LEFT JOIN sl_au_user u on u.id=r.userId GROUP BY r.userId";

		StringBuilder sb = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();

		int count = 0;
		if (StringUtils.hasText(key)) {
			sb.append(" having (u.`name` like '%" + key + "%' or u.sContactType like '%" + key + "%' or u.sex like '%"
					+ key + "%')");

			sb2.append(" having (u.`name` like '%" + key + "%' or u.sContactType like '%" + key + "%' or u.sex like '%"
					+ key + "%')");
		}
		String noLimitSql = sql + sb.toString();
		sb.append(" limit " + start + "," + length);
		sql = sql + sb.toString();
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);

		sql = "select count(*) count from (" + noLimitSql + ")b";

		count = ((Long) jdbcTemplate.queryForMap(sql).get("count")).intValue();
		map.put("data", list);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView) {
		// 移动端
		String device = request.getParameter("device");
		if (this.DEVICE.equals(device)) {
			// 当前登录的游客
			Account myAccount = getMyAccount();

			List<VisitorReward> rewards = visitorRewardService.findRewardOnlyKey(0, Integer.MAX_VALUE,
					myAccount.getUser().getId());
			double sum = 0;
			if (rewards.size() > 0 && rewards != null) {
				for (VisitorReward v : rewards) {
					sum += v.getRewardValue();// 总分
				}
			}

			modelAndView.setViewName("mobile/visitorReward/index");
			modelAndView.addObject("currentVisitor", myAccount);
			modelAndView.addObject("rewards", sum);
			// modelAndView.addObject("details", rewards);
		} else {
			// PC端
			modelAndView = createLayoutView("visitorReward/index");
		}
		return modelAndView;
	}
}
